package top.icecola.community.service;

import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
import top.icecola.community.dao.MessageMapper;
import top.icecola.community.domain.Message;
import top.icecola.community.domain.MessageExample;
import top.icecola.community.util.SensitiveFilter;

import java.util.*;

@Service
public class MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    // 查询当前用户的会话列表,针对每个会话只返回一条最新的私信.
    public List<Message> selectConversations(int userId, int pageNum, int pageSize){
        return messageMapper.selectConversations(userId,pageNum,pageSize);
    }
//    public List<Message> selectConversations(int userId, int pageNum, int pageSize) {
//        PageHelper.startPage(pageNum,pageSize);
//        MessageExample messageExample = new MessageExample();
//        MessageExample.Criteria criteria = messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andFromIdEqualTo(userId);
//        MessageExample.Criteria criteria1 = messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andToIdEqualTo(userId);
//        messageExample.or(criteria1);
//        messageExample.setDistinct(true);
//        messageExample.setOrderByClause("id desc");
//        List<Message> messageList = messageMapper.selectByExample(messageExample);
//        Map<String,Object> map=new HashMap<>();
//        List<Message> list=new ArrayList<>();
//        for(int i=0;i<messageList.size();i++){
//            map.put(messageList.get(i).getConversationId(),messageList.get(i));
//        }
////        for(Message message:messageList){
////            map.put(message.getConversationId(),message);
////        }
//        Iterator<String> iterator = map.keySet().iterator();
//        while (iterator.hasNext()){
//            list.add((Message) map.get(iterator.next()));
//        }
////        System.out.println(map.size());
//        Collections.sort(list, new Comparator<Message>() {
//            @Override
//            public int compare(Message o1, Message o2) {
//                //return o1.para - o2.para;  //升序
//                return o2.getId() - o1.getId(); //降序
//            }
//        });
//        return list;
//    }

    // 查询当前用户的会话数量.
    public int selectConversationCount(int userId) {
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andFromIdEqualTo(userId);
        MessageExample.Criteria criteria1 = messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andToIdEqualTo(userId);
        messageExample.or(criteria1);
        messageExample.setDistinct(true);
        List<Message> messages = messageMapper.selectByExample(messageExample);
        List<Message> messageList = messageMapper.selectByExample(messageExample);
        Map<String,Object> map=new HashMap<>();
        List<Message> list=new ArrayList<>();
        for(int i=0;i<messageList.size();i++){
            map.put(messageList.get(i).getConversationId(),messageList.get(i));
        }
//        for(Message message:messageList){
//            map.put(message.getConversationId(),message);
//        }
        Iterator<String> iterator = map.keySet().iterator();
        while (iterator.hasNext()){
            list.add((Message) map.get(iterator.next()));
        }
//        System.out.println(map.size());
        Collections.sort(list, new Comparator<Message>() {
            @Override
            public int compare(Message o1, Message o2) {
                //return o1.para - o2.para;  //升序
                return o2.getId() - o1.getId(); //降序
            }
        });
        return list.size();
    }

    // 查询某个会话所包含的私信列表.
    public List<Message> selectLetters(String conversationId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andConversationIdEqualTo(conversationId);
        messageExample.setOrderByClause("id desc");
        return messageMapper.selectByExample(messageExample);
    }

    // 查询某个会话所包含的私信数量.
    public int selectLetterCount(String conversationId) {
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdNotEqualTo(1).andConversationIdEqualTo(conversationId);
        messageExample.setOrderByClause("id desc");
        List<Message> messages = messageMapper.selectByExample(messageExample);
        return messages.size();
    }

    // 查询未读私信的数量
    public int selectLetterUnreadCount(int userId, String conversationId) {
        MessageExample messageExample = new MessageExample();
        if(StringUtils.isBlank(conversationId)){
            messageExample.createCriteria().andStatusEqualTo(0).andFromIdNotEqualTo(1).andToIdEqualTo(userId);
            return (int)messageMapper.countByExample(messageExample);
        }else{
            messageExample.createCriteria().andStatusEqualTo(0).andFromIdNotEqualTo(1).andToIdEqualTo(userId).andConversationIdEqualTo(conversationId);
            return (int)messageMapper.countByExample(messageExample);

        }
    }

    // 新增消息
    public void insertMessage(Message message){
        message.setContent(HtmlUtils.htmlEscape(message.getContent()));
        message.setContent(sensitiveFilter.filter(message.getContent()));
        messageMapper.insert(message);
    }

    public void readMessage(List<Integer> ids) {
        for(Integer id:ids) {
            updateStatus(id, 1);
        }
    }

    // 修改消息的状态
    public void updateStatus(Integer id, int status){
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria();
        Message message = messageMapper.selectByPrimaryKey(id);
        message.setStatus(status);
        criteria.andIdEqualTo(id);
        messageMapper.updateByExampleSelective(message,messageExample);
    }

    // 查询某个主题下最新的通知
    public Message findLatestNotice(int userId, String topic){
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdEqualTo(1).andToIdEqualTo(userId).andConversationIdEqualTo(topic);
        List<Message> messages = messageMapper.selectByExample(messageExample);
        if(messages.isEmpty()){
            return null;
        }else{
            return messages.get(messages.size()-1);
        }
    }

    // 查询某个主题所包含的通知数量
    public int findNoticeCount(int userId, String topic){
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdEqualTo(1).andToIdEqualTo(userId).andConversationIdEqualTo(topic);
        List<Message> messages = messageMapper.selectByExample(messageExample);
        if(messages.isEmpty()){
            return 0;
        }else{
            return messages.size();
        }
    }

    // 查询未读的通知的数量
    public int findNoticeUnreadCount(int userId, String topic){
        MessageExample messageExample = new MessageExample();
        MessageExample.Criteria criteria = messageExample.createCriteria().andStatusEqualTo(0).andFromIdEqualTo(1).andToIdEqualTo(userId);
        if(topic!=null){
            criteria.andConversationIdEqualTo(topic);
        }
        List<Message> messages = messageMapper.selectByExample(messageExample);
        if(messages.isEmpty()){
            return 0;
        }else{
            return messages.size();
        }
    }
    // 查询某个主题所包含的通知列表
    public List<Message> findNotices(int userId, String topic, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        MessageExample messageExample = new MessageExample();
        messageExample.createCriteria().andStatusNotEqualTo(2).andFromIdEqualTo(1).andToIdEqualTo(userId).andConversationIdEqualTo(topic);
        messageExample.setOrderByClause("create_time desc");
        return messageMapper.selectByExample(messageExample);
    }
}
